package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.Order;
import com.xxx.entity.dto.OrderDto;
import com.xxx.service.IOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/order")
@Tag(name = "订单管理")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("/add")
    @Operation(summary = "添加订单")
    public Result add(@RequestBody Order order){
        Boolean flag = orderService.save(order);
        return flag ? Result.success("添加订单成功") : Result.error("添加订单失败");
    }

    @GetMapping("/delete/{orderId}")
    @Operation(summary = "删除订单")
    public Result delete(@PathVariable Integer orderId){
        Boolean flag = orderService.removeById(orderId);
        return flag ? Result.success("删除订单成功") : Result.error("不存在该订单Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改订单")
    public Result update(@RequestBody Order order){
        Boolean flag = orderService.updateById(order);
        return flag ? Result.success("修改订单成功") : Result.error("修改订单失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询订单")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
        IPage<Order> page = orderService.page(new Page<>(current,size));
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据订单Id、用户Id条件查询订单 ")
    public Result selectByCondition(@RequestBody OrderDto orderDto){
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
        lqw.eq(orderDto.getUserId()!=null,Order::getUserId,orderDto.getUserId())
                .eq(orderDto.getOrderId()!=null,Order::getOrderId,orderDto.getOrderId());
        IPage<Order> page = orderService.page(new Page<>(orderDto.getCurrent(), orderDto.getSize()),
                lqw);
        return Result.success(page);
    }
}

